package br.com.me.ceap.up;

import br.com.me.ceap.BaseMojo;
import br.com.me.ceap.MECEAPUtils;
import org.apache.cxf.helpers.IOUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.codehaus.plexus.util.FileUtils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * Goal which deploy files to ME CEAP
 *
 * @goal uploadCustomCodeJAR
 * @requiresProject
 */
public class UploadCustomCodeJARMojo extends BaseMojo {
// ------------------------------ FIELDS ------------------------------

    /**
     * Directory containing the generated JAR.
     *
     * @parameter default-value="${project.build.directory}"
     * @required
     */
    private File outputDirectory;

    /**
     * AcrtifactId from Project
     *
     * @parameter default-value="${project.artifactId}"
     * @required
     * @readonly
     */
    private String projectArtifactId;
    /**
     * Version from Project
     *
     * @parameter default-value="${project.version}"
     * @required
     * @readonly
     */
    private String projectVersion;

    /**
     * Read Timeout for uploading Custom Root JAR
     *
     * @parameter default-value="120000"
     */
    private long timeoutUploadJAR;

// --------------------- GETTER / SETTER METHODS ---------------------

    public String getProjectArtifactId() {
        return projectArtifactId;
    }

    public String getProjectVersion() {
        return projectVersion;
    }

    public long getTimeoutUploadJAR() {
        return timeoutUploadJAR;
    }

// ------------------------ INTERFACE METHODS ------------------------


// --------------------- Interface Mojo ---------------------

    public void execute() throws MojoExecutionException, MojoFailureException {
        this.getLog().debug("Preparing to upload CustomRoot's CustomCode JAR.");
        WebClient webClient = MECEAPUtils.forUploadCustomCodeJAR(this, this.getTimeoutUploadJAR());
        String packageFileName;
        byte[] jarFile;
        packageFileName = outputDirectory + File.separator +
                this.getProjectArtifactId() + "-" + this.getProjectVersion() + ".jar";

        jarFile = readPackageJARFile(packageFileName);

        this.getLog().info("Deploying " + packageFileName);
        uploadFileToWebClient(webClient, jarFile, "CustomCode JAR");
    }

// -------------------------- OTHER METHODS --------------------------

    private byte[] readPackageJARFile(final String packageFileName) throws MojoExecutionException {
        final byte[] jarFile;
        try {
            if (!FileUtils.fileExists(packageFileName)) {
                throw new MojoExecutionException(packageFileName + " does not exist. Run package first!");
            }

            this.getLog().debug("Looking for " + packageFileName);
            FileInputStream packageFile = new FileInputStream(packageFileName);
            try {
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                IOUtils.copy(packageFile, byteArrayOutputStream);
                jarFile = byteArrayOutputStream.toByteArray();
            } finally {
                packageFile.close();
            }
        } catch (IOException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }
        return jarFile;
    }
}
